#!/bin/sh
# this script implements the services regulation
# according to what has been decided in the profile

CHKCONFIG=
SYSTEMCTL=
SYSTEMD_USER=

[ ! -x /sbin/chkconfig ] || CHKCONFIG=1
[ ! -x /bin/systemctl  ] || SYSTEMCTL=1

[ -n "$CHKCONFIG$SYSTEMCTL" ] || exit 0

switch() {
	case "$2" in
	on)
		cc=on; sc=enable;;
	off)
		cc=off; sc=disable;;
	unmask)
		sc=unmask;;
	mask)
		sc=mask;;
	esac

	{
		[ -z "$SYSTEMCTL" ] || /bin/systemctl --no-reload $SYSTEMD_USER $sc $1
		[ -z "$CHKCONFIG" ] || /sbin/chkconfig $1 $cc
	} # 2>/dev/null
}

# defaults (most likely features.in ones)
for i in $GLOBAL_DEFAULT_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_DEFAULT_SERVICES_DISABLE; do switch $i off; done

# explicitly specified behaviour (e.g. via conf.d)
for i in $GLOBAL_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_SERVICES_DISABLE; do switch $i off; done

## systemd services
CHKCONFIG=

# defaults
for i in $GLOBAL_DEFAULT_SYSTEMD_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_DEFAULT_SYSTEMD_SERVICES_DISABLE; do switch $i off; done

# explicitly specified behaviour
for i in $GLOBAL_SYSTEMD_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_SYSTEMD_SERVICES_DISABLE; do switch $i off; done
for i in $GLOBAL_SYSTEMD_SERVICES_MASK;    do switch $i mask;   done
for i in $GLOBAL_SYSTEMD_SERVICES_UNMASK;  do switch $i unmask; done

## systemd-logind services
SYSTEMD_USER='--global'

# defaults
for i in $GLOBAL_DEFAULT_SYSTEMD_USER_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_DEFAULT_SYSTEMD_USER_SERVICES_DISABLE; do switch $i off; done

# explicitly specified behaviour
for i in $GLOBAL_SYSTEMD_USER_SERVICES_ENABLE;  do switch $i on;  done
for i in $GLOBAL_SYSTEMD_USER_SERVICES_DISABLE; do switch $i off; done

:
